<?php
namespace Core\Mvc;

use Phalcon\Di;

/**
 * Class EntityManager
 * @package Core\Mvc
 */
class EntityManager
{
    /**
     * @var array
     */
    protected $_entity;
    /**
     * @var \Phalcon\DiInterface
     */
    protected $_di;
    /**
     * @var \Core\Config
     */
    protected $_configServer;

    /**
     * EntityManager constructor.
     */
    public function __construct()
    {
        $this->_di = Di::getDefault();
        $this->_configServer = $this->_di->getShared('config');
        $this->_entity = $this->_configServer->cache('entity');
    }

    /**
     * @param $entity
     * @param null $param
     * @return bool | \Core\Mvc\Entity\EntityInterface | \Core\Mvc\Entity\Entity
     */
    public function get(string $entity, $param = null)
    {
        $entityInfo = $this->_entity[$entity];
        if (!isset($entityInfo['class']) || !class_exists($entityInfo['class'])) {
            return false;
        }
        if (is_null($param)) {
            $entityOb = new $entityInfo['class']();
        } else {
            $callable = $entityInfo['class'] . '::findFirst';
            $entityOb = $callable($param);
        }
        return $entityOb;
    }

    /**
     * @param string $entity
     * @return bool|mixed
     */
    public function info(string $entity)
    {
        if (!isset($this->_entity[$entity])) {
            return false;
        }
        return $this->_entity[$entity];
    }

    /**
     * @param $entity
     * @param null $param
     * @return mixed
     */
    public function find(string $entity, array $param = null)
    {
        $entityInfo = $this->_entity[$entity];
        if (!isset($entityInfo['class']) || !class_exists($entityInfo['class'])) {
            return false;
        }
        $callable = $entityInfo['class'] . '::find';
        return $callable($param);
    }

    /**
     * @param string $entity
     * @param string|null $contentModel
     * @return array|mixed
     */
    public function field(string $entity, string $contentModel = null)
    {
        if (!is_null($contentModel)) {
            return $this->get($entity)->setContentModel($contentModel)->getField();
        }
        $contentModelList = $this->contentModel($entity);
        $output = [];
        foreach ($contentModelList as $name => $contentModel) {
            $field = $this->get($entity)->setContentModel($name)->getField();
            $output = array_merge($output, $field);
        }

        return $output;
    }

    /**
     * @param string $entity
     * @return array|mixed
     * @throws \Exception
     */
    public function contentModel(string $entity)
    {
        if (!isset($this->_entity[$entity])) {
            throw new \Exception('实体不存在：' . $entity);
        }
        $module = $this->_entity[$entity]['module'];
        $configName = 'm.'.$module.'.'.ucfirst($entity).'ContentModel';
        $contentModel = $this->_configServer->get($configName,[]);
        $baseInfo = $this->_configServer->cache($entity.'ContentModel');
        return array_merge($baseInfo,$contentModel);
    }

    /**
     * @param string $entity
     * @return array
     */
    public function contentModelOptions(string $entity)
    {
        $contentModel = $this->contentModel($entity);
        $output = [];
        foreach ($contentModel as $key => $value) {
            $output[$key] = $value['name'];
        }
        return $output;
    }

    /**
     * @param $name
     * @return bool|Entity\Entity|Entity\EntityInterface
     */
    public function __get($name)
    {
        return $this->get($name);
    }
}